package com.treasure.mall.zbqmallmanager.scheduled;

import com.treasure.mall.biz.service.core.BaseConfigService;
import com.treasure.mall.biz.service.core.ScheduledService;
import com.treasure.mall.biz.service.core.integral.IntegralCountService;
import com.treasure.mall.biz.service.core.live.LiveSessionService;
import com.treasure.mall.biz.service.core.statistics.DealAmountStatisticsService;
import com.treasure.mall.biz.service.core.statistics.OrderStatisticsService;
import com.treasure.mall.biz.service.core.user.UserService;
import com.treasure.mall.zbq.common.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/7/23
 */
@Service
@Profile(Constant.PROFILE_DEFAULT)
@Slf4j
public class MScheduleService {
    @Autowired
    private ScheduledService scheduledService;

    @Autowired
    private LiveSessionService LiveSessionService;
    @Autowired
    private BaseConfigService baseConfigService;
    @Autowired
    private UserService userService;
    @Autowired
    private IntegralCountService integralCountService;
    @Autowired
    private OrderStatisticsService orderStatisticsService;
    @Autowired
    private DealAmountStatisticsService dealAmountStatisticsService;


    /**
     * 资金转正任务
     */
    @Scheduled(cron = "0 */10 * * * ?")
    public void convertTask() {
        try {
            scheduledService.convertRebate();
        } catch (Exception ignore) {
        }

        try {
            scheduledService.convertSettlement();
        } catch (Exception ignore) {
        }
    }

    /**
     * 自动签收任务
     */
    @Scheduled(cron = "0 */10 * * * ?")
    public void receiveTask() {
        try {
            scheduledService.receive();
        } catch (Exception ignore) {
        }
    }


    /**
     * 升级达人任务
     */
    @Scheduled(cron = "0 0 4 * * ?")
    public void upgradeDrLevel() {
        try {
            userService.upgradeDrLevel();
        } catch (Exception e) {
        }
    }

    /**
     * 积分统计
     * 每天01点执行
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void integralStatistics() {
        integralCountService.statistics();
    }

    /**
     * 一些统计
     * 每天02点执行
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderStatistics() {
        LocalDate date = LocalDate.now().minusDays(1);
        try {
            dealAmountStatisticsService.statistics(date);
        } catch (Exception ex) {
            log.error("交易统计失败---" + ex.getMessage(), ex);
        }

        try {
            orderStatisticsService.statistics(date);
        } catch (Exception ex) {
            log.error("订单统计失败---" + ex.getMessage(), ex);
        }
    }
}
